package com.shijun.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shijun.bean.CommentBean;
import com.shijun.pullrecyclerviewdemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhushijun on 2018/1/2.
 */

public class CustomListShowView extends LinearLayout {
    private List<CommentBean> data;
    private Context context;
    private LayoutInflater inflater;
    private OnClickItemListener listener;

    public CustomListShowView(Context context, List<CommentBean> data) {
        super(context);
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        if (data == null) {
            this.data = new ArrayList<>();
        } else {
            this.data = data;
        }
        setOrientation(LinearLayout.VERTICAL);
        initVeiw();
    }

    public CustomListShowView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void initVeiw() {
        removeAllViews();
        for (int i = 0; i < data.size(); i++) {
            final int postion = i;
            CommentBean comment = data.get(i);
            RelativeLayout itemView = (RelativeLayout) inflater.inflate(R.layout.inflater_listshow_item, null);
            StringBuffer contentString = new StringBuffer();
            TextView sendNameView = (TextView) itemView.findViewById(R.id.inflater_listshow_item_sendname_tv);
            sendNameView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onClickItem((TextView) view,postion);
                }
            });
            TextView targetNameView = (TextView) itemView.findViewById(R.id.inflater_listshow_item_targetname_tv);
            targetNameView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onClickItem((TextView) view,postion);
                }
            });
            TextView contentView = (TextView) itemView.findViewById(R.id.inflater_listshow_item_content_tv);
            TextView tagView = (TextView) itemView.findViewById(R.id.inflater_listshow_item_to_tag_tv);

            if (comment.getTargetName() == null || "".equals(comment.getTargetName())) {
                contentString.append(comment.getSendName());
                contentString.append("  ");
                tagView.setText("");
            } else {
                contentString.append(comment.getSendName());
                contentString.append("回复");
                contentString.append(comment.getTargetName());
                contentString.append("  ");
                tagView.setText("回复");

            }
            contentString.append(comment.getContent());
            sendNameView.setText(comment.getSendName());
            targetNameView.setText(comment.getTargetName());
            contentView.setText(contentString);
            addView(itemView);
        }
    }

    public void notifyDataSetChanged( List<CommentBean> data){
        this.data.clear();
        this.data.addAll(data);
        initVeiw();
    }

    public void setOnClickItemListener(OnClickItemListener listener) {
        this.listener = listener;
    }

    public interface OnClickItemListener{
        void onClickItem(TextView view,int postion);
    }
}
